package oms.dao;

import java.util.Collection;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import oms.entities.Order;
import oms.entities.User;

@Repository
public class OrderDAOImpl implements OrderDAO {
	
		@Autowired 
		private SessionFactory sessionFactory;
		
		
		// OK
		public void createOrder(Order order) {
		
				sessionFactory.getCurrentSession().save(order);
		}

		//  OK
		public void saveOrder(Long orderId) {
			
				Order order = (Order) sessionFactory.getCurrentSession().load(Order.class, orderId);
				
				if(null != order) {
					sessionFactory.getCurrentSession().update(order);
				} else {
					sessionFactory.getCurrentSession().save(order);
				}
		}

		// OK
		public void updateOrder(Long orderId) {
				
				Order order = (Order) sessionFactory.getCurrentSession().load(Order.class, orderId);
				
				if(null != order) sessionFactory.getCurrentSession().update(order);	
		}

		// OK
		public Order getOrderById(Long orderId) {
			
				Order order = (Order) sessionFactory.getCurrentSession().load(Order.class, orderId);
		
				return order;
		}

		// OK
		public void deleteOrder(Long orderId) {
		
				Order order = (Order) sessionFactory.getCurrentSession().load(Order.class, orderId);
			
				if (null != order) sessionFactory.getCurrentSession().delete(order);
		}
		
		// OK
		@SuppressWarnings("unchecked")
		public List<Order> ordersList() {
		
				return sessionFactory.getCurrentSession().createQuery("from Order").list();
		}

		// NOT OK
		@SuppressWarnings("rawtypes")
		public Collection getOrdersByUser(User user) {
		
				return null;
		}

		@Override
		public Order getOrderByName(String orderName) {
			Order order = (Order) sessionFactory.getCurrentSession().load(Order.class, orderName);
			
			return order;
		}

		@Override
		public Order getOrderByStatus(byte status) {
			Order order = (Order) sessionFactory.getCurrentSession().load(Order.class, status);
			
			return order;
		}

		@Override
		public Order getOrderByAssignee(User user) {
			// TODO Auto-generated method stub
			return null;
		}
}
